﻿using Ext.Net;
using System;
using System.Data;

public partial class MrjSaleRpt : ExtBaseRpt
{
    mrjapp mrj = new mrjapp();
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            try
            {
                InsActLog(mid, "浏览");
                this.txtdaybdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                //BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
                this.comShop.GetStore().DataSource = mydb.DM_GetDateSet("select shopid,shopname from V_ShopList");
                txtdayoperation.GetStore().DataSource = mydb.DM_GetDateSet("select distinct OperationAreaName from V_ShopList");
                txtdaybigarea.GetStore().DataSource = mydb.DM_GetDateSet("select distinct BigAreaName from V_ShopList");
                txtdayarea.GetStore().DataSource = mydb.DM_GetDateSet("select distinct AreaName from V_ShopList");
            }
            catch (Exception ex)
            {
                ShowBox("错误：" + ex.Message);
            }
        }
    }

    /// <summary>
    /// (日)数据分析
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        try
        {
            if (Convert.ToDateTime(txtdaybdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                ShowBox("请输入第一排的本期日期条件！");
                return;
            }
            string strEndDate;
            if (Convert.ToDateTime(txtdayedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
            {
                strEndDate = "";
            }
            else
            {
                strEndDate = Convert.ToDateTime(txtdayedate.Text).ToString("yyyy-MM-dd");
            }
            string strShop = StrSplit(this.selectshop.Text);
            
            string sql = "KM_MrjDaySale_DayTB_Rpt '" + Convert.ToDateTime(txtdaybdate.Text).ToString("yyyy-MM-dd") + "','" + strEndDate
                + "','" + txtdayoperation.Text + "','" + txtdaybigarea.Text + "','" + txtdayarea.Text + "','" + strShop + "','" + txtdayshopname.Text + "','" + Session["UserID"] + "'";
            this.Store1.DataSource = db.DM_GetDateSet(sql);
            this.Store1.DataBind();
            //QueryData(this.Store1, mid, sql);
        }
        catch (Exception ex)
        {
            ShowBox("错误：" + ex.Message);
        }
    }

    /// <summary>
    /// (周)数据分析
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnQureyUser2(object sender, DirectEventArgs e)
    {
        if (txtweek.Text == "" && Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请输入日期查询条件！");
            return;
        }
        if (txtweek.Text != "" && Convert.ToDateTime(txtbdate.Text) != Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("如果按周查询，请删除自定义日期！");
            return;
        }
        string strShop = StrSplit(this.selectshop.Text);
        string strBeginDate, strEndDate, strUpBeginDate, strUpEndDate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strBeginDate = "";
        }
        else
        {
            strBeginDate = txtbdate.Text;
        }
        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strEndDate = "";
        }
        else
        {
            strEndDate = txtedate.Text;
        }
        if (Convert.ToDateTime(txtubdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strUpBeginDate = "";
        }
        else
        {
            strUpBeginDate = txtubdate.Text;
        }
        if (Convert.ToDateTime(txtuedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strUpEndDate = "";
        }
        else
        {
            strUpEndDate = txtuedate.Text;
        }
        string sql = "KM_MrjDaySale_MonthTB_Rpt '" + strBeginDate + "','" + strEndDate + "','" + strUpBeginDate + "','" + strUpEndDate + "','" + txtweek.Text + "','" + txtmonth.Text + "','"
            + txtdayoperation.Text + "','" + txtdaybigarea.Text + "','" + txtdayarea.Text + "','" + strShop + "','" + txtdayshopname.Text + "','" + Session["UserID"] + "'";
        try
        {
            this.Store4.DataSource = db.DM_GetDateSet(sql);
            this.Store4.DataBind();
        }
        catch (Exception ex)
        {
            ShowBox("查询出错：" + ex.Message);
        }
        //QueryData(this.Store1, mid, sql);
    }

    /// <summary>
    /// (月)数据分析
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnQureyUser3(object sender, DirectEventArgs e)
    {
        if (txtweek.Text == "" && Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请输入日期查询条件！");
            return;
        }
        if (txtweek.Text != "" && Convert.ToDateTime(txtbdate.Text) != Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("如果按周查询，请删除自定义日期！");
            return;
        }
        string strShop = StrSplit(this.selectshop.Text);
        string strBeginDate, strEndDate, strUpBeginDate, strUpEndDate;
        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strBeginDate = "";
        }
        else
        {
            strBeginDate = txtbdate.Text;
        }
        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strEndDate = "";
        }
        else
        {
            strEndDate = txtedate.Text;
        }
        if (Convert.ToDateTime(txtubdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strUpBeginDate = "";
        }
        else
        {
            strUpBeginDate = txtubdate.Text;
        }
        if (Convert.ToDateTime(txtuedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            strUpEndDate = "";
        }
        else
        {
            strUpEndDate = txtuedate.Text;
        }
        string sql = "KM_MrjDaySale_MonthTB_Rpt '" + strBeginDate + "','" + strEndDate + "','" + strUpBeginDate + "','" + strUpEndDate + "','" + txtweek.Text + "','" + txtmonth.Text + "','"
            + txtdayoperation.Text + "','" + txtdaybigarea.Text + "','" + txtdayarea.Text + "','" + strShop + "','" + txtdayshopname.Text + "','" + Session["UserID"] + "'";
        try
        {
            this.Store2.DataSource = db.DM_GetDateSet(sql);
            this.Store2.DataBind();
        }
        catch (Exception ex)
        {
            ShowBox("查询出错：" + ex.Message);
        }
        //QueryData(this.Store1, mid, sql);
    }

    /// <summary>
    /// 日明细数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnQureyUser4(object sender, DirectEventArgs e)
    {
        string strShop = StrSplit(this.selectshop.Text);

        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请输入开始日期！");
            return;
        }
        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请输入结束日期！");
            return;
        }
        var UserID = Session["UserID"];
       //try
       //{
       //    Activity.Log.WriteLog("用户" + UserID + "开始请求月数据：" + DateTime.Now);
       //    DataSet ds = mydb.DM_GetDateSet("KM_MrjSaleKpiDayDtl " + UserID + ",'" + txtbdate.Text + "','" + txtedate.Text + "','" + str + "'");
       //    this.Store3.DataSource = ds;
       //    this.Store3.DataBind();
       //    Session["MrjResultData"] = ds;
       //    Activity.Log.WriteLog("用户" + UserID + "结束请求月数据：" + DateTime.Now);
       //}
       //catch (Exception ex)
       //{
       //    Activity.Log.Error("请求报表数据异常：" + ex);
       //}
		 try
        {
           
			var KM_MrjSaleKpiDayDtl_sql="KM_MrjSaleKpiDayDtl " + UserID + ",'" + txtbdate.Text + "','" + txtedate.Text + "','" + strShop + "'";
			 Activity.Log.WriteLog("用户" + UserID + "开始请求月数据：" + DateTime.Now+",sql="+KM_MrjSaleKpiDayDtl_sql);
            //DataSet ds = mydb.DM_GetDateSet(KM_MrjSaleKpiDayDtl_sql); //链接90
             DataSet ds = db.DM_GetDateSet(KM_MrjSaleKpiDayDtl_sql); //链接88
            this.Store3.DataSource = ds;
            this.Store3.DataBind();
            Activity.Log.WriteLog("用户" + UserID + "结束请求月数据：" + DateTime.Now);
        }
        catch (Exception ex)
        {
            Activity.Log.Error("请求日明细数据异常：" + ex);
        }
        //QueryData(this.Store1, mid, sql);
    }
    protected void GetBigArea(object sender, DirectEventArgs e)
    {
        txtdaybigarea.GetStore().DataSource = mydb.DM_GetDateSet("select distinct BigAreaName from V_ShopList where OperationAreaName ='" + txtdayoperation.Value + "'");
        this.Store_BigAreaName.DataBind();
    }
    protected void GetArea(object sender, DirectEventArgs e)
    {
        txtdayarea.GetStore().DataSource = mydb.DM_GetDateSet("select distinct AreaName from V_ShopList where BigAreaName ='" + txtdaybigarea.Value + "'");
        this.Store_AreaName.DataBind();
    }
    protected void GetShop(object sender, DirectEventArgs e)
    {
        this.comShop.GetStore().DataSource = mydb.DM_GetDateSet("select shopid,shopname from V_ShopList where AreaName='" + txtdayarea.Value + "'");
        this.Store_Shop.DataBind();
    }
    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null, null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid, "xls", e.Xml, this.Response, "每人计零售分析(日)");
    }
    protected void Store2_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid, "xls", e.Xml, this.Response, "每人计零售分析(周)");
    }
    protected void Store3_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid, "xls", e.Xml, this.Response, "每人计零售分析(月)");
    }
    protected void Store4_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid, "xls", e.Xml, this.Response, "每人计零售分析(月)");
    }
    protected void BtnOutExcel(object sender, DirectEventArgs e)
    {
        //导出EXCEL
        string str = StrSplit(this.selectshop.Text);

        if (Convert.ToDateTime(txtbdate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请输入开始日期！");
            return;
        }
        if (Convert.ToDateTime(txtedate.Text) == Convert.ToDateTime("0001-01-01 00:00:00"))
        {
            ShowBox("请输入开始日期！");
            return;
        }
        try
        {
            //DataSet ds = mydb.DM_GetDateSet("KM_MrjSaleKpiDayDtl " + this.UserId + ",'" 
            //    + txtbdate.Text + "','" + txtedate.Text + "','" + str + "'"); //链接90
            DataSet ds = db.DM_GetDateSet("KM_MrjSaleKpiDayDtl " + this.UserId + ",'"
                + txtbdate.Text + "','" + txtedate.Text + "','" + str + "'"); //链接88
            string path = NpoiHelper.ExportByWeb(mid, ds, this.Title);
            this.ResourceManager1.AddScript("outExcle('" + path + "')");
        }
        catch (Exception ex)
        {
            Activity.Log.Error("请求报表数据异常：" + ex);
            ShowBox("导出出错："+ex.Message);
        }
    }
}